package com.bestcem.xm.ticket.dao.converter;

import com.bestcem.xm.ticket.entity.mongo.Notification;
import com.bestcem.xm.ticket.entity.mysql.NotificationDO;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * DO和DTO转换类
 *
 * @author zhangfeng <feng.zhang@idiaoyan.com>
 * @version v1.0
 * @date 2021/9/17 16:10
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class NotificationConverter {

    @Nullable
    public static Notification doToEntity(NotificationDO isDo) {
        if (Objects.isNull(isDo)) {
            return null;
        }

        Notification entity = new Notification();
        entity.setId(isDo.getNotificationId());
        entity.setOrgId(isDo.getOrgId());
        entity.setSurveyId(isDo.getSurveyId());
        entity.setNoDisturb(CommonConverter.stringToList(isDo.getNoDisturb(), String.class));
        entity.setRule(CommonConverter.stringToObject(isDo.getRule(), Notification.Rule.class));
        entity.setWays(CommonConverter.stringToObject(isDo.getWays(), Notification.Way.class));
        entity.setSample(isDo.getSample());

        // 是否开启免打扰 通知提醒新增微信公众号需求 新增字段
        entity.setNoDisturbEnable(isDo.getNoDisturbEnable());

        CommonConverter.setEntity(entity, isDo);
        return entity;
    }

    @Nonnull
    public static List<Notification> doListToDtoList(List<NotificationDO> notificationDOList) {
        List<Notification> list = new ArrayList<>();
        if (Objects.isNull(notificationDOList)) {
            return list;
        }

        for (NotificationDO notificationDO : notificationDOList) {
            Notification notification = doToEntity(notificationDO);
            list.add(notification);
        }
        return list;
    }

    @Nullable
    public static NotificationDO entityToDo(Notification entity) {
        if (Objects.isNull(entity)) {
            return null;
        }

        NotificationDO isDo = new NotificationDO();
        isDo.setNotificationId(entity.getId());
        isDo.setOrgId(entity.getOrgId());
        isDo.setSurveyId(entity.getSurveyId());
        isDo.setNoDisturb(CommonConverter.listToString(entity.getNoDisturb()));
        isDo.setRule(CommonConverter.objectToString(entity.getRule()));
        isDo.setWays(CommonConverter.objectToString(entity.getWays()));
        isDo.setSample(entity.getSample());

        // 是否开启免打扰 通知提醒新增微信公众号需求 新增字段
        isDo.setNoDisturbEnable(entity.getNoDisturbEnable());

        CommonConverter.setDo(entity, isDo);
        return isDo;
    }
}
